<template>
  <div>
    <a-modal
      centered
      :title="showCode"
      :width="1000"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :span="10">
              <a-form-item label="编号">
                <a-input placeholder="请输入编号" v-model="queryParam.code"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="10">
              <a-form-item label="名称">
                <a-input placeholder="请输入名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="8">
                    <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                      <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                    </span>
            </a-col>

          </a-row>
        </a-form>
      </div>
      <!-- table区域-begin -->
      <div>
        <a-table
          size="small"
          bordered
          rowKey="code"
          :columns="columns1"
          :dataSource="dataSource1"
          :pagination="ipagination"
          :loading="loading"
          :scroll="{ y: 240 }"
          :rowSelection="{type: ismutiple ? 'radio':'checkbox',selectedRowKeys: selectedRowKeys,onSelectAll:onSelectAll,onSelect:onSelect,onChange: onSelectChange}"
          :customRow="onClickRow"
          @change="handleTableChange">
        </a-table>
      </div>
      <!-- table区域-end -->


    </a-modal>
  </div>
</template>

<script>
import { filterObj } from '@/utils/util'
import { postAction } from '@/api/manage'
import { PageListMixin } from '@/mixins/PageListMixin'

export default {
  name: 'codeConfigModal',
  mixins: [PageListMixin],
  props: {
    showCode: {
      type: String,
      default: '关联门店'
    },
    ismutiple: {
      type: Boolean,
      default:true
    },
  },
  data() {
    return {
      names: [],
      visible: false,
      roleCode: '',
      isfilterRole: 0,
      placement: 'right',
      description: '',
      userNameList: [],
      terminalCode: '',
      // 查询条件
      queryParam: {},
      // 表头
      columns1: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 50,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        { title: '编号', dataIndex: 'code', align: 'center', width: '30%', widthRight: '70%' },
        { title: '名称', dataIndex: 'name', align: 'center', width: '35%' },
        {
          title: '类型', dataIndex: 'type', align: 'center', width: '35%',
          customRender: function(text) {
            if (text === 1) {
              return '门店'
            } else {
              return '经销商'
            }
          }
        }
      ],
      resultob: null,
      //数据集
      dataSource1: [],
      dataSource2: [],
      isorter: {
        column: 'createTime',
        order: 'desc'
      },
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      url: {
        list: window._CONFIG['domianURL'] + '/cnTerminalController/getPageCustAndTermList'
      }
    }
  },
  created() {
    this.loadData(1)
  },
  methods: {
    searchQuery() {
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      let that = this
      that.$emit('ok', that.resultob,that.selectionRows)
      that.searchReset(0)
      that.close()
    },
    // add() {
    //   this.visible = true;
    // },
    loadData(arg) {
      if (this.terminalCode != null && this.terminalCode != '') {
        this.queryParam.terminalCode = this.terminalCode
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()//查询条件
      params.isfilterRole = this.isfilterRole
      this.loading = true
      var that = this
      this.dataSource1 = []
      postAction(this.url.list, params).then((res) => {
        if (res.success) {
          that.dataSource1 = res.result.records
          that.ipagination.total = res.result.total
          that.loading = false
        }
      })

    },
    getQueryParams() {
      var param = Object.assign({}, this.queryParam, this.isorter)
      param.field = this.getQueryField()
      param.current = this.ipagination.current
      param.size = this.ipagination.pageSize
      return filterObj(param)
    },
    getQueryField() {
      //TODO 字段权限控制
    },
    onSelectAll(selected, selectedRows, changeRows) {
      this.resultob = selectedRows[0]
    },
    onSelect(record, selected) {
      this.resultob = record
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectedRows
      this.resultob = selectedRows[0]
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    handleDelete: function(record) {
    },
    showModal(userType, terminalCode) {
      this.terminalCode = terminalCode
      this.visible = true
      this.queryParam.type = userType
      if (terminalCode != null && terminalCode != '') {
        this.queryParam.terminalCode = terminalCode
      }
      this.loadData(1)
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.loadData()
    },
    // customRowCode(cord) {
    //   return:{
    //     on:{
    //       click:() => {
    //         console.log(cord)
    //       }
    //     }
    //   }
    // },
    close() {
      this.searchReset(0)
      this.visible = false
    }
  }
}
</script>
<style lang="less" scoped>
.ant-card-body .table-operator {
  margin-bottom:0px;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 15px;
  padding-bottom: 15px;
}

.anty-row-operator button {
  margin: 0 5px
}

.ant-btn-danger {
  background-color: #ffffff
}

.ant-modal-cust-warp {
  height: 100%
}

.ant-modal-cust-warp .ant-modal-body {
  height: calc(100% - 110px) !important;
  overflow-y: auto
}

.ant-modal-cust-warp .ant-modal-content {
  height: 90% !important;
  overflow-y: hidden
}
</style>
